package com.example.bbs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bbs.entity.Admin;
import com.example.bbs.entity.Article;
import com.example.bbs.mapper.AdminMapper;
import com.example.bbs.mapper.ArticleMapper;
import com.example.bbs.service.ArticleService;
import com.example.bbs.util.ConstUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/5/5 19:08
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<Article> getAllArts() {
        return articleMapper.selectListWithName();
    }

    @Override
    public String addArticle(Article article) {
        if (StringUtils.isEmpty(article.getImg())) {
            article.setImg(ConstUtil.DEFAULT_ARTICLE_IMG);
        }
        article.setView(1);
        article.setSupport(0);
        article.setDate(LocalDate.now());
        int insert = articleMapper.insert(article);
        if (insert > 0) {
            return "200";
        }
        return "500";
    }

    @Override
    public Article selectArticleById(int id) {
        return articleMapper.selectWithNameById(id);
    }

    @Override
    public List<Article> selectArtsByTag(int id) {
        List<Article> articles = articleMapper.selectArtsByTagId(id);
        if (articles.size() == 0) {
            articles = articleMapper.selectListWithName();
        }
        return articles;
    }
}
